<?php

namespace Biz\service\wiki;

use Biz\cache\wiki\WikiBasicCounter;
use Biz\cache\wiki\WikiFavorite;
use Biz\model\brandActivity\BlindBoxActivity as mBlindBoxActivity;
use Biz\model\ShopFavorite as mShopFavorite;
use Biz\model\store\StoreBasic as mStoreBasic;
use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Biz\model\wiki\WikiBuyModel;
use Biz\model\wiki\WikiList as mWikiList;
use Biz\module\Classes\wiki\Infos;
use Biz\module\Classes\wiki\SpuToWiki;
use Biz\module\Classes\wiki\Wiki as clsWiki;
use Biz\pockets\constInterface\WikiFields;
use Biz\pockets\serviceTrait\_Async;
use Biz\service\brand\BlindBoxActivity;
use Biz\service\brand\Brand as sBrand;
use Biz\service\Category;
use Biz\service\wikiRanking\Ranking;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\ExposeLog;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ImgUrl;

class Wiki implements WikiFields
{
    use _Async;


    const WIKI_STYLE_GOOD_THING = 1;


    /**
     * 获取wiki 列表（基础信息列表）
     * @param $wikiIds 商品id
     * @param int $uid 当前用户uid
     * @return array
     * @throws \Exception
     */
    public static function getWikiBaseList($wikiIds, $uid = 0)
    {
        $list = $wikiIds ? Infos::infos($wikiIds, self::FIELDS_GROUP_BASE_LIST, $uid) : [];
        if (HhzClient::isLogExpose()) {
            ExposeLog::recordWikiLog(array_values($list));
        }
        return $list;
    }

    /**
     * 获取wiki 列表
     * @param array $wikiIds 商品id
     * @param int $uid 当前用户uid
     * @param string $fieldsGroup
     * @return array
     */
    public static function getWikiList($wikiIds, $uid = 0, $fieldsGroup = self::FIELDS_GROUP_LIST)
    {
        $list = $wikiIds ? Infos::infos($wikiIds, $fieldsGroup, $uid) : [];
        //增加失效连接跳转地址
        if (HhzClient::isLogExpose()) {
            ExposeLog::recordWikiLog(array_values($list));
        }
        return $list;
    }


    /**
     * 获取完整的wiki信息列表
     * @param $wikiIds 商品id
     * @param int $uid 当前用户uid
     * @return array
     * @throws \Exception
     */
    public static function getCompleteWikiList($wikiIds, $uid = 0)
    {
        $list = $wikiIds ? Infos::infos($wikiIds, self::FIELDS_GROUP_COMPLETE_LIST, $uid) : [];
        if (HhzClient::isLogExpose()) {
            ExposeLog::recordWikiLog(array_values($list));
        }
        return $list;
    }

    /**
     * 获取wiki基础信息
     * @param $wikiId
     * @param int $currentUid
     * @return array
     * @throws \Exception
     */
    public static function getWikiBaseInfo($wikiId, $currentUid = 0)
    {
        $result = Infos::infos([$wikiId], self::FIELDS_GROUP_BASE_DETAIL, $currentUid);
        if (HhzClient::isLogExpose()) {
            ExposeLog::recordWikiLog(array_values($result));
        }
        return $result;
    }

    /**
     * 根据basicid获取wiki基础信息列表
     * @param array $basic_ids
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public static function getWikiBaseListByBasicIds(array $basic_ids, $uid = 0)
    {
        $result = self::getWikiListByBasicIds($basic_ids, $uid, self::FIELDS_GROUP_BASE_LIST);
        return $result;
    }

    public static function getWikiByLinkForApi($content, $type, $objType = 1, $objId = null, $isPreCreate = true)
    {
        $wikiInfo = self::getWikiByLink($content, $objType, null, $isPreCreate);
        if (!empty($wikiInfo)) {
            $result = [
                'goods_id' => $wikiInfo['id'],
                'title' => $wikiInfo['title'],
                'category_name' => isset($wikiInfo['channel_category_name']) ? $wikiInfo['channel_category_name'] : '',
                'brand_name' => isset($wikiInfo['channel_brand']) ? $wikiInfo['channel_brand'] : '',
                "min_price" => isset($wikiInfo['min_price']) ? $wikiInfo['min_price'] : 0,
                "max_price" => isset($wikiInfo['max_price']) ? $wikiInfo['max_price'] : 0,
                'spec_img_url' => isset($wikiInfo['cover_img']) ? $wikiInfo['cover_img'] : '',
                'wiki_status' => $wikiInfo['status']
            ];
            //添加basic info
            if ($wikiInfo['basic_title'] || $wikiInfo['basic_id']) {
                $result['basic_title'] = $wikiInfo['basic_title'] ?: '';
                $result['basic_id'] = $wikiInfo['basic_id'] ?: 0;
                $result['basic_state'] = $wikiInfo['basic_state'];
            }
            //添加basic info
            if ($result['brand_name'] == 'other/其他') {
                $result['brand_name'] = '';
            }
            if (!$result['spec_img_url']) {
                $result['spec_img_url'] = Infos::WIKI_DEFAULT_IMG_3X;
            }
            if ($wikiInfo['brand_id']) {
                $brandInfo = sBrand::getBrandInfoById($wikiInfo['brand_id']);
                if ($brandInfo && $brandInfo['brand_name']) {
                    $result['brand_name'] = $brandInfo['brand_name'];
                }
            }
            if ($wikiInfo['category_id']) {
                $categoryInfo = \Doraemon\model\Category::getCataInfoById($wikiInfo['category_id']);
                if ($categoryInfo && $categoryInfo['cate_name']) {
                    $result['category_name'] = $categoryInfo['cate_name'];
                }
            }
            return $result;
        } else if (is_null($wikiInfo)) {
            $msg = '链接无效，请试试其他链接吧';
            throw new \Exception($msg, -1);
        } else {
            $msg = '无法生成商品链接，请重试';
            $msg = ($type == 1) ? '只能填写链接' : $msg;
            throw new \Exception($msg, -1);
        }
    }

    /**
     * 根据链接获取wiki信息
     * @param $content
     * @param int $objType
     * @param null $objId
     * @param bool $isPreCreate
     * @return array|bool
     * @throws \Exception
     */
    public static function getWikiByLink($content, $objType = 1, $objId = null, $isPreCreate = true)
    {
        return clsWiki::getWikiByContent($content, $objType, $objId, $isPreCreate);
    }

    /**
     * 添加获取wiki 商品的任务
     * @param $content
     */
    public static function addGetWikiSpuTask($content)
    {
        if ($content) {
            self::_async($content);
        }
    }

    public static function _async_addGetWikiSpuTask($content)
    {
        self::getWikiByLink($content, null);
    }

    public static function getOpenOtherLink($info)
    {
        $infos = [];
        $wikiId = 0;
        if ($info) {
            $wikiId = $info['id'];
            $infos[$wikiId] = $info;
        }
        $infos = $infos ? Infos::getOpenAppLink($infos) : [];
        $result = ($infos && isset($infos[$wikiId]) && isset($infos[$wikiId]['open_other_link'])) ? $infos[$wikiId]['open_other_link'] : [];
        return $result;
    }

    /**
     * 获取wiki详情
     * @param $wikiId
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public static function getDetail($wikiId, $currentUid, $defaultWiki = true)
    {
        if ($defaultWiki) {
            $result = Infos::infos([$wikiId], self::FIELDS_GROUP_DETAIL, $currentUid);
            return ($result && isset($result[$wikiId])) ? $result[$wikiId] : [];
        } else {//黑卡调用，更改使用方法
            $result = Infos::infos([$wikiId], self::FIELDS_GROUP_FUNENG_DETAIL, $currentUid);
            return ($result && isset($result[$wikiId])) ? $result[$wikiId] : [];
        }
    }

    /**
     * 获取wiki详情推荐wiki
     * @param $wikiId
     * @param $uid
     * @param int $limit
     * @return array
     * @throws \Exception
     */
    public static function getRecommendByWikiId($wikiId, $uid, $limit = 20)
    {
        if ($wikiId) {
            $wikiGrpcClient = \Wiki\GrpcClient::getInstance();
            $aRet = $wikiGrpcClient->getRecommendByWikiId([
                "wikiId" => $wikiId,
                "pageSize" => $limit,
                "fieldsGroup" => self::FIELDS_GROUP_MASONRY_LIST,
                "uid" => $uid,
            ]);
            if ($aRet['biz_code'] != 0) {
                return [];
            }
            $list = $aRet['result']['wiki_list'] ?? [];
            $list = $list ? array_column($list, null, 'id') : [];
            return ['list' => $list];
        } else {
            return ['list' => []];
        }
    }


    /**
     * 获取用户的清单列表
     * @param $uid
     * @param int $currUid
     * @param int $page
     * @param int $pageSize
     * @param string $pageName inventory_page：个人清单 publish_relate_wiki:发布器页面
     * @return array
     * @throws \Exception
     */
    public static function getUserWikiList($uid, $currUid = 0, $page = 1, $pageSize = 20, $pageName = 'inventory_page')
    {
        if (!$uid) {
            $uid = $currUid;
        }
        $page = $page ? $page : 1;
        $limit = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $result = mWikiList::getUserWikiList($uid, $limit, [
            'addtime' => 'DESC',
            'id' => 'DESC'
        ]);
        $count = mWikiList::getUserWikiCount($uid);
        $isOver = (count($result) >= $pageSize) ? 0 : 1;
        $wikiIds = array_column($result, 'wiki_id');
        $wikiList = self::getCompleteWikiList($wikiIds, $currUid);
        $list = [];
        $module = ($pageName == 'publish_relate_wiki') ? basicStatSign::MODULE_PUBLISH_RELATE_WIKI_WIKI_CARD : basicStatSign::MODULE_INVENTORY_PAGE_WIKI_CARD;
        foreach ($wikiList as $row) {
            $row['statSign'] = basicStatSign::genStatSign($module, [
                basicStatSign::EXTEND_KEY_OBJ_ID => $row['id'],
                basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI,
            ], ['tab' => 'related']);
            $list[] = $row;
        }
        return [
            'list' => $list,
            'count' => $count,
            'is_over' => $isOver
        ];
    }

    /**
     * 获取用户收藏的wiki列表
     * @param $uid
     * @param $currUid
     * @param int $page
     * @param int $pageSize
     * @param string $pageName inventory_page：个人清单 publish_relate_wiki:发布器页面
     * @return array
     */
    public static function getFavList($uid, $currUid, $page = 1, $pageSize = 20, $pageName = 'inventory_page')
    {
        $page = $page ?: 1;
        $ids = self::getAllFav($uid);
        $favList = $ids ? mShopFavorite::getFavInfosByIds($ids, $page, $pageSize) : [];
        $wikiIds = $favList ? array_column($favList['list'], 'obj_id') : [];
        $list = $wikiIds ? self::getWikiList($wikiIds, $currUid) : [];
        $list = $list ? array_values($list) : [];
        $result = [];
        if ($list) {
            $module = ($pageName == 'publish_relate_wiki') ? basicStatSign::MODULE_PUBLISH_RELATE_WIKI_WIKI_CARD : basicStatSign::MODULE_INVENTORY_PAGE_WIKI_CARD;
            foreach ($list as $row) {
                $row['statSign'] = basicStatSign::genStatSign($module, [
                    basicStatSign::EXTEND_KEY_OBJ_ID => $row['id'],
                    basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI,
                ], ['tab_name' => 'wiki_fav']);
                $result[] = $row;
            }
        }
        return [
            'list' => $result,
            'is_over' => $favList ? $favList['is_over'] : 1
        ];
    }

    /**
     * 获取用户买过的wiki列表
     * @param $uid
     * @param $currUid
     * @param int $page
     * @param int $pageSize
     * @param string $pageName inventory_page：个人清单 publish_relate_wiki:发布器页面
     * @return array
     */
    public static function getBuyList($uid, $currUid, int $page = 1, int $pageSize = 20, $pageName = 'inventory_page'): array
    {
        $where = [
            'uid' => $uid,
            'status' => WikiBuyModel::STATUS_NORMAL,
            'ORDER' => ['edittime' => 'DESC'],
            'LIMIT' => [($page - 1) * $pageSize, $pageSize],
        ];
        $wikiIds = WikiBuyModel::selectByWhere($where, 'wiki_id');
        $isOver = count($wikiIds) < $pageSize ? 1 : 0;
        $list = $wikiIds ? self::getWikiList($wikiIds, $currUid) : [];
        $list = $list ? array_values($list) : [];
        $result = [];
        if ($list) {
            $module = ($pageName == 'publish_relate_wiki') ? basicStatSign::MODULE_PUBLISH_RELATE_WIKI_WIKI_CARD : basicStatSign::MODULE_INVENTORY_PAGE_WIKI_CARD;
            foreach ($list as $row) {
                $row['statSign'] = basicStatSign::genStatSign($module, [
                    basicStatSign::EXTEND_KEY_OBJ_ID => $row['id'],
                    basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI,
                ], ['tab_name' => 'wiki_bought']);
                $result[] = $row;
            }
        }

        return [
            'list' => $result,
            'is_over' => $isOver,
        ];
    }

    public static function getAllFav($uid)
    {
        //查询所有的商品和wiki收藏
        $list = mShopFavorite::getAllFav($uid, [
            mShopFavorite::TYPE_WIKI,
            mShopFavorite::TYPE_GOODS
        ], mShopFavorite::APP_SOURCE_APP);
        $goodsIds = [];
        foreach ($list as $v) {
            if ($v['obj_type'] == mShopFavorite::TYPE_GOODS) {
                $goodsIds[] = $v['obj_id'];
            }
        }
        //查询已经转换wiki的商品
        $map = $goodsIds ? SpuToWiki::getRelateWikiMapByGoodsIds($goodsIds) : [];
        $ids = [];
        foreach ($list as $v) {
            if ($v['obj_type'] == mShopFavorite::TYPE_WIKI) {
                $ids[] = $v['id'];
            }
            if (($v['obj_type'] == mShopFavorite::TYPE_GOODS) && isset($map[$v['obj_id']])) {
                $ids[] = $v['id'];
            }
        }
        return $ids;
    }

    /**
     * 获取wiki 商品基础信息
     * @param $wikiIds
     * @return array
     */
    public static function getWikiBaseInfos($wikiIds)
    {
        $result = $wikiIds ? clsWiki::getWikiBaseInfos($wikiIds) : [];
        return $result;
    }

    public static function getWikiContentInfos($wikiIds)
    {
        $result = $wikiIds ? clsWiki::getWikiContentInfos($wikiIds) : [];
        return $result;
    }


    /**
     * @param $wikiIds
     * @return array
     */
    public static function getWikiBasicIdsByWikiIds($wikiIds)
    {
        $params = array(
            "id" => $wikiIds,
            'status[!]' => mWiki::STATUS_DEL,
            "basic_id[>]" => 0,
            "is_default" => mWiki::IS_DEFAULT
        );

        $wikiInfos = mWiki::selectByWhere($params);

        $basicIds = array();

        if (is_array($wikiInfos) && $wikiInfos) {
            $basicIds = array_column($wikiInfos, "basic_id");
            $basicIds = array_values(array_filter($basicIds));
        }

        return $basicIds;
    }

    /**
     * 根据分类id获取wiki好物推荐
     * @param array $categoryIds
     * @param int $uid
     * @param int $page
     * @param int $pageSize
     * @param array $adList
     * @return array
     * @throws \Exception
     */
    public static function getWikiListByCategoryIds(array $categoryIds, $uid = 0, $page = 0, $pageSize = 20, $adList = [])
    {
        $count = 0;
        $fieldsGroup = self::FIELDS_GROUP_MASONRY_LIST;
        $wikiGrpcClient = \Wiki\GrpcClient::getInstance();
        $wikiIds = [];
        $adWikiIds = [];
        $wikiMap = [];
        $adWikiMap = [];
        if ($categoryIds) {
            $aRet = $wikiGrpcClient->getWikiListByCategoryIds([
                "categoryIds" => $categoryIds,
                "page" => $page,
                "pageSize" => $pageSize,
                "fieldsGroup" => $fieldsGroup,
                "uid" => $uid,
            ]);
            if ($aRet['biz_code'] != 0) {
                return [];
            }
            $count = $aRet['result']['allTotal'] ?? 0;
            $list = $aRet['result']['wiki_list'] ?? [];
            $wikiIds = $list ? array_column($list, 'id') : [];
            $wikiMap = $list ? array_column($list, null, 'id') : [];
        }

        //广告位的wikiId
        if ($adList) {
            $adWikiIds = array_column($adList, 'obj_id');
            $adPosMap = array_column($adList, 'obj_id', 'pos');
            $adWikiMap = self::getWikiList($adWikiIds, $uid, $fieldsGroup);
            $adWikiList = array_values($adWikiMap);
            $adWikiIds = $adWikiList ? array_column($adWikiList, 'id') : [];
        }

        $totalWikiIds = array_merge($wikiIds, $adWikiIds);
        $totalWikiIds = array_unique(array_filter($totalWikiIds));

        //合并广告和正常查询
        $combineList = [];
        $total = count($totalWikiIds);
        foreach (range(1, $total) as $pos) {
            if (isset($adPosMap[$pos])) {
                if (isset($adWikiMap[$adPosMap[$pos]])) {
                    $combineList[] = $adWikiMap[$adPosMap[$pos]];
                }
            } else {
                $tempId = array_shift($wikiIds);
                if (isset($wikiMap[$tempId])) {
                    $combineList[] = $wikiMap[$tempId];
                }
            }
        }

        return [
            'list' => $combineList,
            'is_over' => $count < ($page * $pageSize) ? 1 : 0,
            'count' => $count,
        ];
    }

    /**
     * 获取最近，最热的wiki(好物推荐线，兜底方案)
     * @param $uid
     * @param int $page
     * @param int $pageSize
     * @return array
     * @throws \Exception
     */
    public static function getHotWikiList($uid, $page = 0, $pageSize = 20)
    {
        $page = $page ? $page : 1;
        //grpc 调用
        $wikiGrpcClient = \Wiki\GrpcClient::getInstance();
        $aRet = $wikiGrpcClient->getHotWikiList([
            "uid" => $uid,
            "page" => $page,
            "pageSize" => $pageSize,
            "fieldsGroup" => self::FIELDS_GROUP_MASONRY_LIST
        ]);

        if ($aRet['biz_code'] != 0) {
            return [];
        }
        $count = $aRet['result']['allTotal'] ?? 0;
        $isOver = $count < ($page * $pageSize) ? 1 : 0;
        $list = $aRet['result']['wiki_list'] ?? [];
        return ['list' => $list, 'is_over' => $isOver];
    }


    /*
     * 根据WikiId uid获取关联信息
     * $param $wikiId
     * $param $type 0： 住友说 1：品牌说
     * $return array
     * */
    public static function getRelaObjIdsByWikiId($wikiId, $type = 0)
    {
        $condition['wiki_id'] = $wikiId;
        $brandUid = 0;
        $storeUid = 0;
        $wikiInfos = self::getWikiBaseInfos([$wikiId]);
        if ($wikiInfos) {
            $wikiInfo = isset($wikiInfos[$wikiId]) ? $wikiInfos[$wikiId] : [];
            if ($wikiInfo) {
                if ($wikiInfo['brand_id']) {
                    $brandInfo = sBrand::getBrandsInfoByIds([$wikiInfo['brand_id']]);
                    if ($brandInfo && isset($brandInfo[$wikiInfo['brand_id']]) && $brandInfo[$wikiInfo['brand_id']]['uid']) {
                        $brandUid = $brandInfo[$wikiInfo['brand_id']]['uid'];
                    }
                    if ($wikiInfo['basic_id']) {
                        $wikiIds = mWiki::selectByWhere([
                            'basic_id' => $wikiInfo['basic_id'],
                            'status[!]' => mWiki::STATUS_DEL
                        ], 'id');
                        $wikiIds[] = $wikiId;
                        $condition['wiki_id'] = $wikiIds;
                    }
                } else if ($wikiInfo['store_id']) {
                    $storeInfo = mStoreBasic::getStoreInfoById($wikiInfo['store_id']);
                    $storeUid = !empty($storeInfo) ? $storeInfo['uid'] : 0;
                }
            }
            if ($type == 1) {//品牌说
                if ($brandUid) {
                    $condition['uid'] = $brandUid;
                } else {
                    return [];
                }
            } else if ($type == 2) {//门店说
                if ($storeUid) {
                    $condition['uid'] = $storeUid;
                } else {
                    return [];
                }
            } elseif ($type == 0) {//住友说
                $filterUid[] = $brandUid;
                $filterUid[] = $storeUid;
                $filterUid = array_filter($filterUid);
                if ($filterUid) {
                    $condition['filter_uid'] = $filterUid;
                }
            }
            return mObjRelateWiki::getObjIdsByCondition($condition);
        } else {
            return [];
        }
    }

    /**
     *  获取wiki关联内容数量（住友说过滤b级用户以及官号）
     * @param $wikiId
     * @param int $type
     * @return array|bool
     */
    public static function getRelateObjNumByWikiId($wikiId, $type = 0)
    {
        $condition['wiki_id'] = $wikiId;
        $brandUid = 0;
        $storeUid = 0;
        $wikiInfos = self::getWikiBaseInfos([$wikiId]);
        if ($wikiInfos) {
            $wikiInfo = isset($wikiInfos[$wikiId]) ? $wikiInfos[$wikiId] : [];
            if ($wikiInfo) {
                if ($wikiInfo['brand_id']) {
                    $brandInfo = sBrand::getBrandsInfoByIds([$wikiInfo['brand_id']]);
                    if ($brandInfo && isset($brandInfo[$wikiInfo['brand_id']]) && $brandInfo[$wikiInfo['brand_id']]['uid']) {
                        $brandUid = $brandInfo[$wikiInfo['brand_id']]['uid'];
                    }
                    if ($wikiInfo['basic_id']) {
                        $wikiIds = mWiki::selectByWhere([
                            'basic_id' => $wikiInfo['basic_id'],
                            'status[!]' => mWiki::STATUS_DEL,
                        ], 'id');
                        $wikiIds[] = $wikiId;
                        $condition['wiki_id'] = $wikiIds;
                    }
                } else {
                    if ($wikiInfo['store_id']) {
                        $storeInfo = mStoreBasic::getStoreInfoById($wikiInfo['store_id']);
                        $storeUid = !empty($storeInfo) ? $storeInfo['uid'] : 0;
                    }
                }
            }
            if ($type == 1) {//品牌说
                if ($brandUid) {
                    $condition['uid'] = $brandUid;
                } else {
                    return 0;
                }
            } else {
                if ($type == 2) {//门店说
                    if ($storeUid) {
                        $condition['uid'] = $storeUid;
                    } else {
                        return 0;
                    }
                } elseif ($type == 0) {//住友说
                    $filterUid[] = $brandUid;
                    $filterUid[] = $storeUid;
                    $filterUid = array_values(array_filter($filterUid));
                    if ($filterUid) {
                        $condition['filter_uid'] = $filterUid;
                    }
                }
            }
            return count(mObjRelateWiki::getObjIdsByCondition($condition)) ?? 0;
        } else {
            return 0;
        }
    }


    /**
     * 获取wiki推荐语
     * @param array $info
     * @return string
     * @throws \Exception
     */
    public static function getWikiRecommendation($info)
    {
        return clsWiki::getWikiRecommendation($info);
    }


    /**
     * 根据basicid获取wiki信息
     * @param array $basic_ids
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public static function getWikiListByBasicIds(array $basic_ids, $uid = 0, $fieldsGroup = self::FIELDS_GROUP_LIST)
    {
        //grpc 调用
        $wikiGrpcClient = \Wiki\GrpcClient::getInstance();
        $aRet = $wikiGrpcClient->getWikiListByBasicIds([
            "basicIds" => $basic_ids,
            "fieldsGroup" => $fieldsGroup,
            "uid" => $uid
        ]);

        if ($aRet['biz_code'] != 0) {
            return [];
        }
        $list = $aRet['result']['list'] ?? [];
        $result = [];
        if ($list) {
            $wikiList = array_values($list);
            $wikiList = array_column($wikiList, null, 'basic_id');
            foreach ($basic_ids as $basic_id) {
                $wikiInfo = $wikiList[$basic_id] ?? [];
                if ($wikiInfo) {
                    $result[$basic_id] = $wikiInfo;
                }
            }
        }
        return $result;
    }

    public static function getWikiListByBasicIdsForRanking(array $basic_ids, $uid = 0)
    {
        return self::getWikiListByBasicIds($basic_ids, $uid, self::FIELDS_GROUP_MASONRY_LIST);
    }

    /**
     * wiki中间页兜底方案获取wiki_ids
     * @param $wikiId
     * @param int $limit
     * @return array
     */
    public static function getRecommendWikiIds($wikiId, $limit = 20)
    {
        $wikiIds = [];
        if ($wikiId) {
            $info = mWiki::get(['channel_shop_id', 'category_id', 'channel', 'id'], ['id' => $wikiId]);
            $shopId = $info['channel_shop_id'];
            $categoryId = $info['category_id'];
            $channel = $info['channel'];
            $field = [
                'id',
            ];
            $list = mWiki::selectByWhere([
                'channel' => $channel,
                'channel_shop_id' => $shopId,
                'id[!]' => $wikiId,
                'LIMIT' => [
                    0,
                    $limit
                ],
                'ORDER' => ['id' => 'DESC']
            ], $field);

            if (count($list) < $limit) {
                $ids = [];
                if ($list) {
                    $ids = array_column($list, 'id');
                }
                $limit = $limit - count($list);
                $where = [
                    'id[!]' => $wikiId,
                    'category_id' => $categoryId,
                    'category_id[!]' => 0,
                    'status' => mWiki::STATUS_CHECKED,
                    'LIMIT' => [
                        0,
                        $limit
                    ],
                    'ORDER' => ['id' => 'DESC']
                ];
                if ($ids) {
                    $where['id[!]'] = $ids;
                }
                $list2 = mWiki::selectByWhere($where, $field);
                $list = array_merge($list, $list2);
            }
            $wikiIds = array_column($list, 'id');
        }
        return $wikiIds;
    }


    /**
     * 根据品牌与basic_id 获取 imgs
     * @param $basic_id
     * @param $brand_id
     * @return array
     */
    public static function getWikiListByBasicId($basic_id, $brand_id): array
    {
        $where = [
            'status[!]' => mWiki::STATUS_DELETE,
            'basic_id' => $basic_id,
            'brand_id' => $brand_id,
            'is_default' => mWiki::IS_DEFAULT
        ];

        $id = mWiki::get('id', $where);

        if (is_null($id)) {
            return [];
        }
        $imgs = \Biz\model\wiki\WikiContent::get(['imgs'], ['wiki_id' => $id]);
        if (is_null($imgs)) {
            return [];
        }
        $data = json_decode($imgs['imgs'], true);

        return array_slice($data, 0, 5);

    }

    /**
     * 查询官方默认wiki信息通过Basic_ids;
     * @param $basic_ids
     * @param string $field
     * @return array|bool
     */
    public static function getWikiInfoByBasicIds($basic_ids, $field = '*')
    {
        $where = [
            'status[!]' => mWiki::STATUS_DELETE,
            'basic_id' => $basic_ids,
            'is_default' => mWiki::IS_DEFAULT,
            'is_official' => 1
        ];

        return mWiki::selectByWhere($where, $field);
    }
}
